﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Mathematics;
using System.Drawing;
namespace lesson1
{
    class GDI3DClass
    {


        public static List<System.Drawing.Point> Project(List<Vector4> vs, float d)
        {

            Matrix m = new Matrix(0);
            m[0, 0] = m[1, 1] = m[3, 3] = 1;
            m[2, 3] = 1 / d;
            List<System.Drawing.Point> result = new List<System.Drawing.Point>();
            foreach (var item in vs)
            {
                Vector4 projP = Vector4.Transform(item, m);
                result.Add(new System.Drawing.Point((int)(projP.X / projP.W), (int)(projP.Y / projP.W)));
                //result.Add(Vector4.Transform(item, m));
            }
            return result;

        }
        public static List<Vector4> RotateX(List<Vector4> vs, Vector4 center,double angle)
        {
            
            Matrix m = new Matrix(0);
            //Matrix.RotationX((float)angle, out m);
            //Matrix t = Matrix.Translation(-center.X, -center.Y, center.Z);
            //Matrix fh=new Matrix(0);
            //Matrix.Multiply(ref t, ref m,out fh);

            m[0, 0] = m[3, 3] = 1;
            m[1, 1] = (float)Math.Cos(angle);
            m[1, 2] = (float)Math.Sin(angle); 
            m[2, 1] = -(float)Math.Sin(angle); 
            m[2, 2] = (float)Math.Cos(angle);
            List<Vector4> rmove = Move(vs, -center.X, -center.Y, -center.Z);
            List<Vector4> result = new List<Vector4>();
            foreach (var item in rmove)
            {
                result.Add(Vector4.Transform(item, m));
            }
            return Move(result, center.X, center.Y, center.Z);

        }
        public static List<Vector4> Move(List<Vector4> vs, float tx, float ty, float tz)
        {

            Matrix m = new Matrix(0);
            m[0, 0] = m[1, 1] = m[2, 2] = m[3, 3] = 1;
            m[3, 0] = tx;
            m[3, 1] = ty;
            m[3, 2] = tz;
            List<Vector4> result = new List<Vector4>();
            foreach (var item in vs)
            {
                result.Add(Vector4.Transform(item, m));
            }
            return result;

        }
        public static void DrawCube(Graphics g, List<System.Drawing.Point> vs)
        {
            for (int i = 0; i < 4; i++)
            {
                PointF p1 = new PointF(vs[i].X,vs[i].Y );
                PointF p2 = new PointF(vs[(i+1)%4].X, vs[(i + 1)% 4].Y);
                g.DrawLine(Pens.Black, p1, p2);

                p1 = new PointF(vs[i+4].X, vs[i+4].Y);
                p2 = new PointF(vs[(i + 1) % 4+4].X, vs[(i + 1) % 4+4].Y);
                g.DrawLine(Pens.Black, p1, p2);

                p1 = new PointF(vs[i].X, vs[i].Y);
                p2 = new PointF(vs[i + 4].X, vs[i + 4].Y);
                g.DrawLine(Pens.Black, p1, p2);

            }
        }
    }
}
